import clientSDK from './client_sdk.js'
import bus from './bus.js'


//换取对应资源URL字段
function change_urltype(data){
   switch(data){
     case 1:
       return '/resource/getHeatMapsById'
       break
     case 2:
        return '/resource/getTimeLapsesById'
       break
     case 3:
       return '/resource/getPartialsById'
       break
     case 4:
        return '/resource/getVirtualRealitysById'
       break
     case 5:
       return '/resource/getRotation360sById'
       break
     case 6:
        return '/resource/getInnerStructsById'
       break
     case 7:
       return '/resource/getPolyvVideosById'
       break
     case 8:
        return '/resource/getZoomInsById'
       break
     case 9:
       return '/resource/getMultiAnglesById'
       break
     case 10:
        return 'resource/getQuizsById'
       break
       case 13:
           return 'resource/getCollectionById'
           break
   }
}

//分发下载
const fenfa_params = function(ziyuan_list,course_list){
    window.download_num = 0
    window.download_infos = []


    let the_lesson_id = localStorage.course_id

    if(course_list[0].id){
      course_list.forEach((course_ppt)=>{
          course_ppt.zip_url = course_ppt.url
          course_ppt.meta_type = "courseware"
          course_ppt.books_id = "booksid_"+localStorage.publisher_id
          course_ppt.book_item_id = localStorage.book_id
          course_ppt.lesson_id = the_lesson_id
          course_ppt.file_size = course_ppt.fileSize
          course_ppt.meta_id = course_ppt.id
          course_ppt.show_name = course_ppt.title
          course_ppt.single = "true"
          course_ppt.updated_at = ''
          window.download_infos.push(course_ppt)
      })
    }

    if(ziyuan_list){
       ziyuan_list.forEach((item)=>{
         download_num++
         item.books_id = "booksid_"+localStorage.publisher_id
         item.book_item_id = localStorage.book_i

         download_resource_info(item)
       })
    }else{
       download_lesson_resources(window.download_infos)
    }


}

//下载资源详细信息
const download_resource_info = function(data){
    //console.log(data)

    let downurl = change_urltype(data.resMetaInfoVO.res_type)

    window.$Vue.$http.post(downurl,{
        id:data.resMetaInfoVO.meta_id
    })
    .then((res)=>{
        if(res.data.code == 0){
          window.download_num--
          res.data.returnValue.intro = data.intro
          res.data.returnValue.show_name = data.name
          clientSDK.saveResourceDetail(res.data.returnValue.meta_info.meta_id,res.data.returnValue.meta_info.meta_type,res.data.returnValue)

          let cc = res.data.returnValue.meta_info
          cc.show_name = data.name
          cc.zip = clientSDK.rescContextUrl() + cc.zip
          cc.zip_url = cc.zip
          cc.intro = data.intro
          cc.books_id = "booksid_"+localStorage.publisher_id
          cc.book_item_id = localStorage.book_id
          cc.lesson_id = data.lesson_id
          cc.single = "true"
          window.download_infos.push(cc)

        }
        if(window.download_num == 0){

           download_lesson_resources(window.download_infos)
        }
    })
    .catch((err)=>{
      console.log(err)
    })
}

//初始化课件-下载单个课件
const download_a_courseware_resc = function(course_ppt){
        course_ppt.zip_url = course_ppt.url
        course_ppt.meta_type = "courseware"
        course_ppt.books_id = "booksid_"+localStorage.publisher_id
        course_ppt.book_item_id = localStorage.book_id
        course_ppt.lesson_id = localStorage.course_id
        course_ppt.file_size = course_ppt.fileSize
        course_ppt.meta_id = course_ppt.id
        course_ppt.show_name = course_ppt.title
        course_ppt.single = "true"
        course_ppt.updated_at = ''
        course_ppt.intro = ''
        let current_download_batch_no = clientSDK.downloadCoursewareResc(course_ppt)
}

//同步资源-下载多个课件
const download_ulf_resc = function(data){
   //console.log(data)
   let infos = []
   data.forEach((item)=>{
      item['meta_type'] = 'ppt'
      item['meta_id'] = '0'
      item['zip_url'] = item['downloadUrl'] || ''
      item['show_name'] = item['displayName']
      item['file_name'] = item['displayName']
      item['file_size'] = item['fileSize']
      item.intro = ''
      item.updated_at = ''
      item.books_id = "booksid_"+localStorage.publisher_id
      item.lesson_id = localStorage.course_id

      infos.push(item)
   })

   console.log(infos)
   let current_download_batch_no = clientSDK.downloadUserLessonFile(infos)
   console.log(current_download_batch_no)

   if (current_download_batch_no.substring(0, 5) == 'error') {
     console.log('同步课件失败')
     return false
   }
   window.$Vue.$message({
     message:'同步云端课件成功',
     center:true,
     type:'success'
   })
   return true
}

//同步资源-分发下载
const fenfa_tongbuziyuan = function(ziyuan_list){
    window.download_num = 0
    window.download_infos = []

    ziyuan_list.forEach((item)=>{
      download_num++
      item.books_id = "booksid_"+localStorage.publisher_id
      item.book_item_id = localStorage.book_id
      item.lesson_id = localStorage.course_id

      tongbu_download_resource_info(item)
    })
}

//同步资源-下载资源详细信息
const tongbu_download_resource_info = function(data){
    //console.log(data)

    let downurl = change_urltype(data.res_type)

    window.$Vue.$http.post(downurl,{
        id:data.meta_id
    })
    .then((res)=>{
        if(res.data.code == 0){
          window.download_num--
          res.data.returnValue.intro = data.intro
          res.data.returnValue.show_name = data.name
          clientSDK.saveResourceDetail(res.data.returnValue.meta_info.meta_id,res.data.returnValue.meta_info.meta_type,res.data.returnValue)

          let cc = res.data.returnValue.meta_info
          cc.show_name = data.name
          cc.zip = clientSDK.rescContextUrl() + cc.zip
          cc.zip_url = cc.zip
          cc.intro = data.intro
          cc.books_id = "booksid_"+localStorage.publisher_id
          cc.book_item_id = localStorage.book_id
          cc.lesson_id = localStorage.course_id
          cc.single = "true"
          window.download_infos.push(cc)

        }
        if(window.download_num == 0){
          // console.log('is 0')
          // console.log(window.download_infos)
          // console.log( JSON.stringify(window.download_infos) )
          // return
           download_lesson_resources(window.download_infos,'true')
          //console.log(cc)
        }
    })
    .catch((err)=>{
      console.log(err)
    })
}

//云资源下载-下载资源详细信息
const yunziyuan_download_resource_info = function(data){
   //console.log('yunziyuan_download_resource_info')
   //return
   let downurl = change_urltype(data.res_type)
   window.$Vue.$http.post(downurl,{
       id:data.meta_id
   })
   .then((res)=>{
       if(res.data.code == 0){
         res.data.returnValue.intro = data.intro
         res.data.returnValue.show_name = data.show_name

         //console.log(data)
         //console.log(res.data.returnValue)
         clientSDK.saveResourceDetail(res.data.returnValue.meta_info.meta_id,res.data.returnValue.meta_info.meta_type,res.data.returnValue)

         //return
         let cc = res.data.returnValue.meta_info
         cc.show_name = data.show_name
         cc.zip = clientSDK.rescContextUrl() + cc.zip
         cc.zip_url = cc.zip
         cc.intro = data.intro
         cc.books_id = "booksid_"+localStorage.publisher_id
         cc.book_item_id = localStorage.book_id
         cc.lesson_id = localStorage.course_id
         //cc.single = "true"

         //console.log(cc)
         //console.log( JSON.stringify(cc)  )
         //return

         localStorage.yunziyuan_metainfoid = res.data.returnValue.meta_info.id

         //console.log('next ______ downloadCloudResc')
         let current_download_batch_no = clientSDK.downloadCloudResc(cc)
         //console.log(current_download_batch_no)
         if (current_download_batch_no.substring(0, 5) == 'error') {

           return false
         }
       }
   })
   .catch((err)=>{
    console.log(err)
   })
}


//批量下载
const download_lesson_resources = function(data,type = 'false'){
	  //console.log('download_lesson_resources')
    //return
    //console.log( JSON.stringify(data) )

    //console.log(type)
    //clientSDK.cancelDownload()
	  let download_batch_no = clientSDK.downloadResourceZipList(data,type);
	  if (download_batch_no == 'error') {
	      console.log('download_lesson_resources 失败')
	      return 'failed';
	  }
	  //console.log('download_lesson_resources 成功')
    let cc = {
      CBType:'download-progress',
      LessonId:data[0].lesson_id,
      Value:0,
    }

    if(localStorage.downbook_type == 'course'){
      bus.$emit('course_down_progress',cc)
      return
    }
    if(localStorage.downbook_type == 'book'){
      bus.$emit('book_down_progress',cc)
      return
    }
    if(localStorage.downbook_type == 'update'){
      bus.$emit('update_down_progress',cc)
      return
    }


	 // return 'started';
}

//批量下载回调
const lesson_resource_download_callback = function (param_obj) {
	      console.log('lesson_resource_download_callback')
        //console.log(param_obj.CBType)
        //console.log(param_obj)
        //return
        if (param_obj.CBType == 'download-completed') {
           // console.log('is download-completed')
            //console.log(param_obj)

            if(param_obj.IsSyncResc){  //同步资源回调
               bus.$emit('yun_ziyuan',param_obj)
               return
            }

            window.$Vue.$db_sdk.getData('pcDB','bookcontents',param_obj.BookItemId)
            .then((res)=>{
                //console.log(res)
                let dd1 = ''
                res.data.forEach((item,index)=>{
                  let cc = item.lessonVOList.findIndex( (element)=>{
                     if(element.id == param_obj.LessonId){
                        dd1 = element.resVersion

                        let obj = {
                          id:param_obj.LessonId,
                          bookid:param_obj.BookItemId,
                          resVersion:dd1
                        }
                        let cc = 'downcourse'+localStorage.userId

                        window.$Vue.$db_sdk.update('pcDB',cc,obj,{index_name:'bookid',index_unique:false})
                     }
                   })
                })
            })
            .catch((err)=>{
              console.log(err)
            })


            if(localStorage.downbook_type == 'course'){
              bus.$emit('course_down_progress',param_obj)
              return
            }
            if(localStorage.downbook_type == 'book'){
              bus.$emit('book_down_progress',param_obj)
              return
            }
            if(localStorage.downbook_type == 'update'){
              bus.$emit('update_down_progress',param_obj)
              return
            }

        	  return
        }
        if (param_obj.CBType == 'download-progress') {
           if(param_obj.IsSyncResc){  //同步资源回调
               console.log('yunzifyuan_down_progress')
               bus.$emit('yunzifyuan_down_progress',param_obj)
               return
            }

           if(localStorage.downbook_type == 'course'){
              bus.$emit('course_down_progress',param_obj)
              return
            }
            if(localStorage.downbook_type == 'book'){
              bus.$emit('book_down_progress',param_obj)
              return
            }
            if(localStorage.downbook_type == 'update'){
              bus.$emit('update_down_progress',param_obj)
              return
            }
        }
        if (param_obj.CBType == 'download-part-completed') {
        	  return
        }
}

export default{
	download_a_courseware_resc:download_a_courseware_resc,
	download_lesson_resources:download_lesson_resources,
  lesson_resource_download_callback:lesson_resource_download_callback,
  download_resource_info:download_resource_info,
  fenfa_params:fenfa_params,
  download_ulf_resc:download_ulf_resc,
  fenfa_tongbuziyuan:fenfa_tongbuziyuan,
  yunziyuan_download_resource_info:yunziyuan_download_resource_info
}











































